﻿if (typeof Admin == "undefined") Admin = {};
if (typeof Admin.Contact == "undefined") Admin.Contact = {};

Admin.Contact.ContactList = {
    Init: function () {
        var thisObj = this;
        thisObj.Grid.Init();
        thisObj.EventClick();
        $("button").button();
        var cmaskdate = new CMaskDate({
            dateTimePicker: true

        });
        cmaskdate.Init($("#txtEndDate,#txtStartDate"));
        thisObj.DropdownChecklist.Init();
    },
    DropdownChecklist: {
        Instance: null,
        Init: function () {
            $("#FormEdit,#FormFilter").show();
            var thisObj = this;
            thisObj.Instance = new CDropDownCheckList({ width: 200, maxDropHeight: 310, firstItemChecksAll: true });
            thisObj.Instance.Init("#FormEdit select[multiple]");
            $("#FormEdit,#FormFilter").hide();
        },
        Clear: function () {
            var thisObj = this;
            $("#FormEdit select[multiple]").find("option").removeAttr("selected");
            thisObj.Refresh();
        },
        Refresh: function () {
            var thisObj = this;
            if (thisObj.Instance) {
                thisObj.Instance.Refresh();
            }
        }
    },
    Grid: {
        ID: "#GridContents",
        PagesID: "#GridPages",
        Init: function () {
            var thisObj = this;
            var options = {
                url: 'Service/ContactListservice.ashx?Action=GET',
                datatype: "json",
                colNames: [
                    'ID', 'Tên', 'Điện Thoại', 'Email','Message' ,
                    'Last Update', 'Trạng thái','', 'Xử lý'
                ],
                colModel: [
                    { name: 'ID', index: 'ID', width: 50, frozen: true, hidden: true, key: true, hidedlg: true },
                    { name: 'Name', index: 'Code', width: 100, frozen: true, hidedlg: true },
                    { name: 'Phone', index: 'Name', width: 150, frozen: true },
                    { name: 'Email', index: 'Name', width: 150, frozen: true },
                    { name: 'Message', index: 'Name', width: 150, frozen: true },
                    {
                        name: 'LastModfify',
                        width: 120, align: 'center'
                    },
                   { name: 'StatusString', index: 'StatusString', width: 70, align: 'center' },
                    { name: 'Status', index: 'Status', width: 70, align: 'center', hidden: true },
                   { name: 'Action', index: 'Action', width: 55, align: 'center' }
                ],
                afterInsertRow: function (rowid, aData, addData) {


                    var action = $.string.Format("<img class='imgAction' src='../../Images/16x16/edit.png' onclick=\"ShowEdit('{0}')\"/><img class='imgAction' src='../../Images/16x16/cancel.png' onclick=\"DeleteNews('{0}')\"/>", rowid);
                    $(thisObj.ID).jqGrid('setCell', rowid, 'Action', action);
                    var iconStatus = 's_enable.png';
                    if (aData.Status == '-1')
                        iconStatus = 's_delete.png';
                    if (aData.Status == '0')
                        iconStatus = 's_visible.png';
                    var StatusString = $.string.Format("<img class='imgAction' src='../../Images/16x16/{0}' onclick=\"ActionStatus('{1}',{2})\"/>", iconStatus, rowid, aData.Status);
                    $(thisObj.ID).jqGrid('setCell', rowid, 'StatusString', StatusString);
                },
                gridComplete: function () {
                    //$('.imgProjectImage').tooltip({
                    //    delay: 0,
                    //    track: true,
                    //    showURL: false,
                    //    bodyHandler: function () {
                    //        return $("<img/>").attr("src", this.src);
                    //    }
                    //});
                },
                onCellSelect: function (rowid, iCol, cellcontent, e) {

                },
                onSelectRow: function (rowid, status, e) {

                },
                ondblClickRow: function (rowid, iRow, iCol, e) {

                },                
                multiselect: true,
                height: 'auto',
                rownumbers: true,
                autowidth: true,
                //forceFit: true,
                shrinkToFit: false,
                rowNum: 20,
                pager: thisObj.PagesID
            };
            thisObj.GridObj = new CGrid(options);
            thisObj.GridObj.Init(thisObj.ID);
            thisObj.GridObj.NavGrid();
            thisObj.GridObj.FrozenColumns();
        },
        Reload: function () {
            var thisObj = this;
            if (thisObj.GridObj)
                thisObj.GridObj.ReloadGrid();
        },
        SelectRow: function () {
            var thisObj = this;
            if (thisObj.GridObj) {
                return thisObj.GridObj.SelectRow();
            }
            return null;
        }
    },
    DialogEdit: {
        ID: "#FormEdit",
        Dialog: null,
        Init: function (action, opts) {
            var thisObj = this;
            thisObj.ClearForm();
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
            else {
                thisObj.Dialog = new CDialog({
                    title: "Sửa dự án",
                    width: 800,
                    position: {
                        my: 'top',
                        at: 'top'
                    },
                    open: function () {
                       
                       
                    }
                });
                thisObj.Dialog.Init(thisObj.ID);
            }
            var buttonText = "";
            switch (action) {
                case "INSERT":
                    thisObj.Dialog.SetTitle("Thêm đại lý");
                    $(thisObj.ID).find("[data-role='INSERT']").show();
                    $(thisObj.ID).find("#txtAddress").focus();
                    buttonText = "Add";
                    $("#txtStatus").attr('checked', 'checked');
                    break;
                case "UPDATE":
                    thisObj.Dialog.SetTitle("Sửa: " + opts.Address  );
                    $.each(opts, function (key, value) {
                        switch (key) {
                            case "Thumbnail":
                            case "Logo":
                            case "Image1":
                            case "Image2":
                            case "Image4":
                            case "Image3":
                                value = $(value).attr("FileName") || "";
                                $(thisObj.ID).find("[data-field=" + key + "]").val(value);
                                break;
                            case 'Status':
                                if (value == "1")
                                    $("#txtStatus").attr('checked', 'checked');
                                else
                                    $("#txtStatus").removeAttr('checked');
                                break;
                            default:
                                $(thisObj.ID).find("[data-field=" + key + "]").val(value);
               

                        }

                    });
                  
                    buttonText = "Update";
                    break;
            }
            var buttons = [
                        {
                            text: buttonText,
                            click: function (e) {
                                Admin.Contact.ContactList.Update(action, opts);
                                thisObj.Close();
                            }
                        },
                        {
                            text: "Cancel",
                            click: function (e) {
                                thisObj.Close();
                            }
                        }

            ];
            $(thisObj.ID).dialog('option', 'buttons', buttons);
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Open();
            }
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Dialog) {
                thisObj.Dialog.Close();
            }
        },
        ClearForm: function () {
            $("#FormEdit").find("[data-field]").each(function () {
                var $this = $(this);
                $this.val("");
                if ($this.is("select")) {
                    var value = $this.find("option[Default-Value='1']").attr("value");
                    $this.val(value);
                }

            });
            $("#txtScreening").val(3);
            $("#FormEdit").find("[data-role='INSERT']").hide();
            $("#gift_items").html("");
        //    Axis.Admin.ListProject.DropdownChecklist.Clear();
        }
    },
    Update: function (type, opts) {
        var thisObj = this;
        var data = {};
        switch (type) {
            case "INSERT":
            case "UPDATE":
                $("#FormEdit").find("[data-field]").each(function () {
                    var $this = $(this);
                    if ($this.attr("data-field")) {
                        var key = $this.attr("data-field");
                        var value = $this.val();
                        if ($this.attr('type') == 'checkbox') {
                            if ($this.is(':checked'))
                                value = 1;
                        }                       

                        if ($this.attr("role") == "datepicker") {
                            value = $.CFormat.FormatDate(value, "yyyy/MM/dd HH:mm", "vi-VN", "en-US");
                        }
                       
                        data[key] = value;
                    }
                });
                break;
            case "DELETE":
                data.ID = opts.ID;
                break;
        }
        data.Action = type;
        if (typeof USER_ID != "undefined") {
            data.UserID = USER_ID;
            data.SecrectToken = SECRECT_TOKEN;
        }
        $.Ajax({
            url: 'Service/ContactListservice.ashx',
            type: 'POST',
            data: data,
            success: function (data, textStatus, jqXHR) {
                if (data) {
                    data = eval("(" + data + ")");
                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
            },
            error: function (jqXHR, textStatus, errorThrown) {
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });
    },
    DialogFilter: {
        Init: function () { }
    },
    EventClick: function () {
        var thisObj = this;
        $("#imgUserAdd").click(function () {
            thisObj.DialogEdit.Init("INSERT");
        });
        $("#imgUserEdit").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID)
                thisObj.DialogEdit.Init("UPDATE", rowData);
            else alert("Please Select Row");
        });
        $("#imgUserDel").click(function () {
            var rowData = thisObj.Grid.SelectRow();
            if (rowData && rowData.ID) {
                $.CAlert.Confirm({
                    Title: "Xóa loại rượu",
                    Content: $.string.Format("Bạn có chắc muốn loại rượu: [{0}].", rowData.Name)
                }, function (isOK) {
                    if (isOK)
                        thisObj.Update("DELETE", rowData);
                });
            }
            else {
                $.CAlert.Alert({
                    Title: "Xóa dự án",
                    Content: "Vui lòng chọn dự án cần xóa."
                });
            }

        });
        $("#imgUserFilter").click(function () {
            thisObj.DialogFilter.Init();
        });
        $("#btnSelectLogo").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Logo/";
            finder.startupFolderExpanded = true;
            finder.selectActionFunction = function (fileUrl) {
                $('#txtLogo').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnSelectThumbnail").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Thumbnail";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtThumbnail').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnSelectImage1").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Image";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtImage1').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnSelectImage2").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Image";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtImage2').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnSelectImage3").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Image";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtImage3').val(fileUrl);
            };
            finder.popup();
        });
        $("#btnSelectImage4").click(function () {
            var finder = new CKFinder();
            finder.resourceType = 'Images';
            finder.startupPath = "Images/Product/Image";
            finder.selectActionFunction = function (fileUrl) {
                $('#txtImage4').val(fileUrl);
            };
            finder.popup();
        });
    },
    ChangeStatus: function (projectID, status) {
        var thisObj = this;
        var pData = { ProjectID: projectID, Status: status };
        $.ajax({
            url: 'Service/ManagerQuestionService.asmx/ChangeStatus',
            type: 'POST',
            data: JSON.stringify(pData),
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (pData, textStatus, jqXHR) {
                $("body").removeClass('iMarkets-overlay-modal');
                var data = eval("(" + pData.d + ")");
                if (data) {

                    if (data.IsSuccessfull) {
                        thisObj.Grid.Reload();
                    }
                }
            },
            beforeSend: function (jqXHR, settings) {
                $("#ddlGiftLottory").html('');
                $("#ddlGift").html('');
                $("body").addClass('iMarkets-overlay-modal');
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //  overlay.Hide();
                $("body").removeClass('iMarkets-overlay-modal');
            },
            complete: function (XMLHttpRequest, textStatus) {
                // overlay.Hide();
            }
        });
    }


};
$(function () {

    Admin.Contact.ContactList.Init();
    //$("#imgUserAdd").click();
});
function ShowEdit(rowID) {
    var thisObj = Admin.Contact.ContactList;
    var rowData = $(thisObj.Grid.ID).jqGrid('getRowData', rowID);
    if (rowData && rowData.ID)
        thisObj.DialogEdit.Init("UPDATE", rowData);
}
function FormatGridDescription(cellvalue, options, rowObject) {
    if (cellvalue.indexOf("project_desc_panel") == -1) {
        return '<div class="project_desc_panel">' + cellvalue + '</div>';
    }
    else
        return cellvalue;

}
